package com.sellspringcloud.server.utils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author
 * @create 2019-08-10 18:52
 **/
public class CookiesUtil {

    public static void addCookie(HttpServletResponse response,
                                 String cookiesName, String value, int expire) {
        Cookie cookie = new Cookie(cookiesName, value);
        cookie.setPath("/");
        cookie.setMaxAge(expire);
        response.addCookie(cookie);
    }

    public static String getCookie(HttpServletRequest request, String cookieName){
        String result = null;
        Cookie[] cookies = request.getCookies();
        if(cookies == null || cookies.length < 1){
            return result;
        }
        for (int i = 0; i < cookies.length; i++) {
            if(cookies[i].getName().equals(cookieName)){
                result = cookies[i].getValue();
                break;
            }
        }
        return result;
    }

}